
Partial Class DashboardControls_PendingSpendingPlan
    Inherits System.Web.UI.UserControl
    Implements IDashBoard

    Dim titleLabelString As String = ""
    Dim pageSizeValue As Integer = 0

    Protected Property GridViewSortDirection() As String
        Get
            If ViewState("SortDirection") Is Nothing Then
                ViewState("SortDirection") = SortDirection.Ascending
            End If
            Return CType(ViewState("SortDirection"), SortDirection)
        End Get
        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set
    End Property

    Public Property Title() As String Implements IDashBoard.Title
        Get
            If Me.TitleLabel IsNot Nothing Then
                Return TitleLabel.Text
            Else
                Return titleLabelString
            End If
        End Get
        Set(ByVal value As String)
            If TitleLabel IsNot Nothing Then
                TitleLabel.Text = value
            End If
            titleLabelString = value
        End Set
    End Property

    Public Property SortDirection() As System.Web.UI.WebControls.SortDirection Implements IDashBoard.SortDirection
        Get
            Return GridViewSortDirection
        End Get
        Set(ByVal value As System.Web.UI.WebControls.SortDirection)
            GridViewSortDirection = value
        End Set
    End Property

    Public Property PageNumber() As Integer Implements IDashBoard.PageNumber
        Get
            Return Me.PendingSpendingPlanGridView.PageIndex
        End Get
        Set(ByVal value As Integer)
            Me.PendingSpendingPlanGridView.PageIndex = value
        End Set
    End Property

    Public Property PageSize() As Integer Implements IDashBoard.PageSize
        Get
            If Not Me.PendingSpendingPlanGridView Is Nothing Then
                Return Me.PendingSpendingPlanGridView.PageSize
            Else
                Return pageSizeValue
            End If
        End Get
        Set(ByVal value As Integer)
            If Not Me.PendingSpendingPlanGridView Is Nothing Then
                Me.PendingSpendingPlanGridView.PageSize = value
                Me.NumberOfRowsDropDownList.SelectedValue = value.ToString()
            End If
            pageSizeValue = value
        End Set
    End Property

    Public Property SortField() As String Implements IDashBoard.SortField
        Get
            If ViewState("SortField") Is Nothing Then
                Return ""
            Else
                Return ViewState("SortField")
            End If
        End Get
        Set(ByVal value As String)
            ViewState("SortField") = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Init
        If Not Me.IsPostBack Then
            AddHandler NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load


        If Not String.IsNullOrEmpty(titleLabelString) Then
            Me.TitleLabel.Text = titleLabelString
        End If

        If pageSizeValue > 0 Then
            Me.PageSize = pageSizeValue
        End If

        If Not Me.IsPostBack Then
            GridViewSettingController.SetGridViewValues(Me, 5, "DisplayLastNameFirst", WebControls.SortDirection.Ascending)
            Me.BindGrid()
        End If
    End Sub

    Protected Sub NumberOfRowsDropDownList_SelectedIndexChanged(ByVal sender As Object, _
                                                                ByVal e As System.EventArgs) _
                                                                Handles NumberOfRowsDropDownList.SelectedIndexChanged

        If NumberOfRowsDropDownList.SelectedValue = "-1" Then
            Me.PendingSpendingPlanGridView.AllowPaging = False
        Else
            PendingSpendingPlanGridView.AllowPaging = True
            PendingSpendingPlanGridView.PageSize = NumberOfRowsDropDownList.SelectedValue
            PendingSpendingPlanGridView.PageIndex = 0
        End If

        Me.BindGrid()

    End Sub

    Public Sub BindGrid() Implements IDashBoard.BindGrid

        Me.PendingSpendingPlanGridView.DataSource = Cdm.DataCore.Budget.PendingSpendingPlan.ListBySort(Me.SortField, Me.SortDirection.Equals(SortDirection.Ascending))
        Me.PendingSpendingPlanGridView.DataBind()
        GridViewSettingController.SaveSetting(Me)

    End Sub

    Protected Sub grvNotifications_PageIndexChanging(ByVal sender As Object, _
                                                     ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) _
                                                     Handles PendingSpendingPlanGridView.PageIndexChanging

        Me.PageNumber = e.NewPageIndex
        Me.BindGrid()

    End Sub

    Public Sub LoadControl1(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Implements IDashBoard.LoadControl

        Me.Page_Load(sender, e)

    End Sub

    Protected Sub PendingSpendingPlanGridView_RowCommand(ByVal sender As Object, _
                                                         ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
                                                         Handles PendingSpendingPlanGridView.RowCommand

        If e.CommandName = "ViewFile" Then
            Response.Redirect("~/Case/EditFile.aspx?ActiveTab=Financial&participantId=" & e.CommandArgument)
        End If

    End Sub

    Protected Sub PendingSpendingPlanGridView_RowDataBound(ByVal sender As Object, _
                                                           ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
                                                           Handles PendingSpendingPlanGridView.RowDataBound
        If e.Row.DataItemIndex = 0 Then
            AccessibilityController.AddImageToSortableGridHeaders(PendingSpendingPlanGridView)
        End If
        If e.Row.RowType = DataControlRowType.DataRow Then
            CType(e.Row.Cells(3).FindControl("ViewLinkButton"), LinkButton).CommandArgument = Me.PendingSpendingPlanGridView.DataKeys(e.Row.RowIndex).Values(0)
        End If

    End Sub

    Protected Sub PendingSpendingPlanGridView_Sorting(ByVal sender As Object, _
                                                      ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) _
                                                      Handles PendingSpendingPlanGridView.Sorting
        Dim sortField As String = e.SortExpression
        
        If GridViewSortDirection = SortDirection.Ascending And sortField.Equals(Me.SortField) Then
            GridViewSortDirection = SortDirection.Descending
            Me.PendingSpendingPlanGridView.DataSource = Cdm.DataCore.Budget.PendingSpendingPlan.ListBySort(sortField, False)
        Else
            GridViewSortDirection = SortDirection.Ascending
            Me.PendingSpendingPlanGridView.DataSource = Cdm.DataCore.Budget.PendingSpendingPlan.ListBySort(sortField, True)
        End If

        If Me.NumberOfRowsDropDownList.SelectedValue <> -1 Then
            Me.PendingSpendingPlanGridView.PageSize = Me.NumberOfRowsDropDownList.SelectedValue
        End If
        Me.PendingSpendingPlanGridView.DataBind()
        Me.SortField = sortField

        GridViewSettingController.SaveSetting(Me)

    End Sub

End Class
